// ===============================================
// btn_ex
// 该按钮是 拓展按钮的 包括了 显示Icon 的按钮
// Create by Will on 19/3/2024 19:33
// Copyright Will.All rights reserved.
// ===============================================

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

import '../img_ex/img_ex.dart';

class BtnEx extends StatefulWidget {
  BtnEx({
    super.key,
    this.text,
    this.textStyle,
    this.width,
    this.height,
    this.iconName,
    this.iconSize,
    this.bgColor,
    this.borderRadius,
    this.radius = 10,
    this.decoration,
    this.onTap,
    this.isLoading = false,
    this.iconSpacing = 10,
    this.loadingWidget,
    this.stopLoading = false,
  });

  String? text;
  final TextStyle? textStyle;
  final double? width;
  final double? height;
  //支持本地的图片
  final String? iconName;
  final double? iconSize;
  final double? iconSpacing;
  //如果不设定
  final Color? bgColor;
  //自定义的描述
  final Decoration? decoration;

  ///自定义的边角描述
  final BorderRadius? borderRadius;
  // 图片的圆角
  final double radius;

  //点击事件
  final GestureTapCallback? onTap;

  final bool isLoading;

  bool stopLoading;

  final Widget? loadingWidget;

  @override
  State<BtnEx> createState() => _BtnExState();
}

class _BtnExState extends State<BtnEx> {
  bool isLoading = false;
  String text = '';

  @override
  void initState() {
    super.initState();
    text = widget.text ?? '';
  }

  @override
  Widget build(BuildContext context) {
    return InkWell(
      onTap: () {
        setState(() {
          isLoading = true;
          if (widget.isLoading && isLoading) {
            widget
              ..text = 'loading'
              ..stopLoading = false;
          }
        });
        widget.onTap?.call();
      },
      child: SizedBox(
        width: widget.width,
        height: widget.height,
        child: Center(
          child: Container(
            decoration: widget.decoration ??
                BoxDecoration(
                  borderRadius: widget.borderRadius ??
                      BorderRadius.circular(widget.radius),
                  color:
                      widget.bgColor ?? CupertinoTheme.of(context).primaryColor,
                ),
            width: widget.width,
            height: widget.height,
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                if (widget.iconName != null) ...<Widget>[
                  ImgEx(
                    url: widget.iconName!,
                    width: widget.iconSize,
                    height: widget.iconSize,
                  ),
                  SizedBox(width: widget.iconSpacing),
                ],
                if (widget.isLoading &&
                    isLoading &&
                    !widget.stopLoading) ...<Widget>[
                  widget.loadingWidget ??
                      SizedBox.fromSize(
                        size: Size.square(
                            widget.height == null ? 20 : widget.height! / 3),
                        child: const CircularProgressIndicator(
                          valueColor: AlwaysStoppedAnimation(Colors.white),
                          strokeWidth: 2.0,
                        ),
                      ),
                  SizedBox(width: widget.iconSpacing),
                ],
                Text(
                  widget.text ?? '',
                  style: widget.textStyle,
                )
              ],
            ),
          ),
        ),
      ),
    );
  }
}
